#include"LinkStack.h"
#include"LinkQueue.h"

#include<iostream>

struct Item 
{
    public:
    Item(){};
    Item(const int &id,const std::string &name,const double& price):id(id),name(name),price(price){}
    
    
    int id;
    std::string name;
    double price;
};


class ShoppingCart
{
private:
    Item item;

    LinkStack cart;
    
    // LinkQueue pay;

    LinkQueue queue;
    

public:
    // 把商品添加到购物车
    void ADD();

    // 把刚刚加进去的商品撤销
    void Remove();
    
    //展示购物车 
    void show(); 

    // 把购物车的商品 按照先放先付的方式进行付款
    void pay();

    // 展示支付历史
    void payhistory();
};


void ShoppingCart::ADD()
{
       Item * goods = new Item;
        
        std::cout<<"请输入商品名:";
        std::cin>>goods->name;
        std::cout<<"请输入商品的号码:";
        std::cin>>goods->id;
        std::cout<<"请输入商品的价格:";
        std::cin>>goods->price;

        cart.Push(goods);
        std::cout<<"您已成功把商品"<<goods->name<<"添加进购物车\n";

        delete goods;
    
}

void ShoppingCart::Remove()
{
    int c = 0;
    std::cout<<"请问您是要删除刚刚存放在购物车中的商品吗？\n";
    std::cout<<"如果需要删除请输入: 1 \n";
    
    std::cin>>c;
    
    if(c == 1 && !cart.IsEmpty())
    {
       Item *i = (Item*)cart.GetTop();
       delete i;
       cart.Pop(); 
       std::cout<<"您已经成功删除刚刚放进购物车的商品\n";
       
    }
    else
        return;

}

void ShoppingCart::show()
{
    LinkStack temp;
    if(cart.IsEmpty())
    {
        std::cout<<"您的购物车已经空啦!\n";

    }
    else
    {
        std::cout<<"您的购物车有如下商品";
        while (!cart.IsEmpty())
        {
            temp.Push(*cart.GetTop());
            cart.Pop();  
        }
        
        while(!temp.IsEmpty())
        {
            Item *i = (Item*)*temp.GetTop();
            
            std::cout<<"商品名:"<<i->name<<" 商品编号:"<<i->id<<" 商品价格:"<<i->price<<std::endl;
            cart.Push(i);
            delete i;
            temp.Pop();
        }

    }

}





void ShoppingCart::pay()
{
    int count =0;
    int num = 0;
    LinkStack temp;
    
    // std::cout<<"请输入你想要结算的商品 号码: ";
    // std::cin>>num;
    
    // while(!cart.IsEmpty())
    // {
    //     Item *i = (Item*)*temp.GetTop();
    //     if(i->id == num)
    //     {
            
    //     }


    // }

   
    while (!cart.IsEmpty())
    {
        temp.Push(*cart.GetTop());

        cart.Pop();  
    }




    while(!temp.IsEmpty())
    {
        Item *i = (Item*)*temp.GetTop();
        count += i->price;
        queue.Push(i);
        
        delete i;
        
        temp.Pop();
    }
    std::cout<<"您需要支付的金额是："<<count<<std::endl;



}







void ShoppingCart::payhistory()
{
    LinkQueue q;
    while(!queue.IsEmpty())
    {
        q.Push(*queue.GetFront());
        queue.Pop();
    }

    while(!q.IsEmpty())
    {
        Item *i = (Item*)*q.GetFront();
        std::cout<<"商品名:"<<i->name<<" 商品编号:"<<i->id<<" 商品价格:"<<i->price<<std::endl;
        queue.Push(i);
        delete i;
        q.Pop();
    }

}


int main()
{
    ShoppingCart c;
    int choice = 0;
    std::cout<<"欢迎打开在线购物车管理系统!\n";

    while(1)
    {
        std::cout<<" 1. 添加商品!\n";
        std::cout<<" 2. 撤销上一次添加!\n";
        std::cout<<" 3. 查看购物车内容!\n";
        std::cout<<" 4. 结算商品!\n";
        std::cout<<" 5. 查看支付历史记录!\n";
        std::cout<<" 0. 退出!\n";
        std::cin>>choice;

        switch (choice)
        {
        case 1:
                c.ADD();
            break;
        
        case 2:
                c.Remove();
            break;
        
        case 3:
                c.show();
            break;
        
        case 4:
                c.pay();
            break;
        
        case 5:
                c.payhistory();
            break;
        case 0:
            exit(-1);
        default:
            std::cout<<"请输入正确的选项!\n";
            break;
        }
    }





    return 0;
}